package com.wechat.servlet.FriendRequest;

import com.alibaba.fastjson.JSONArray;
import com.wechat.DAO.DaoImpl.FriendRequestDAOIpml;
import com.wechat.DAO.DaoImpl.UserInfoDAOIpml;
import com.wechat.DAO.FriendRequestDAO;
import com.wechat.DAO.UserInfoDAO;
import com.wechat.pojo.FriendRequest;
import com.wechat.pojo.UserInfo;
import com.wechat.servlet.Utils.Base64Encoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FriendRequestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("当前收到一条post请求");
        try {
            String SID = request.getParameter("SID");
            String RID = request.getParameter("RID");
            UserInfoDAO userInfoDAO = new UserInfoDAOIpml();
            FriendRequestDAO friendRequestDAO = new FriendRequestDAOIpml();
            if(friendRequestDAO.queryByBothID(SID,RID)!=null){
                response.getWriter().write("wait");
                return;
            }
            UserInfo userInfo = userInfoDAO.query(SID);
            FriendRequest friendRequest = new FriendRequest(System.currentTimeMillis(),
                    SID,userInfo.getNickname(),RID,
                    "你好，交个朋友",
                    userInfo.getHPUrl(),
                    "unknow",false);
            System.out.println(friendRequest);
            friendRequestDAO.insert(friendRequest);
            response.getWriter().write("success");
        }catch (Exception e){
            e.printStackTrace();
            response.getWriter().write("fail");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("当前收到一条get请求");
        String RID = request.getParameter("username");
        String type = request.getParameter("type");
        FriendRequestDAO friendRequestDAO =  new FriendRequestDAOIpml();
        List<FriendRequest> friendRequests = new ArrayList<>();
        try{
            friendRequests = friendRequestDAO.queryByRID(RID);
        }catch (Exception e){
            e.printStackTrace();
            response.getWriter().write("fail");
            return;
        }
        if(friendRequests.isEmpty()){
            response.getWriter().write("null");
            return;
        }
        if(type.equals("new")){
            String base64 = null;
            List<FriendRequest> newFriendRequests = new ArrayList<>();
            //进行查询和转化操作
            try {
                //判断是否为未处理
                for(FriendRequest friendRequest : friendRequests){
                    if(friendRequest.getIF_Receive())
                        continue;
                    if(friendRequest.getStatus().equals("unknow")){
                        newFriendRequests.add(friendRequest);
                    }
                }
                if (newFriendRequests.isEmpty()){
                    response.getWriter().write("fail");
                    return;
                }
                JSONArray jsonArray = (JSONArray) JSONArray.toJSON(newFriendRequests);
                base64 = Base64Encoder.encode(jsonArray.toString());
            }catch (Exception e){
                e.printStackTrace();
                response.getWriter().write("fail");
                return;
            }
            //判断是否有结果
            if(base64==null || base64.isEmpty()){
                response.getWriter().write("fail");
                return;
            }else {
                response.getWriter().write(base64);
            }
        }else if(type.equals("all")){
            JSONArray jsonArray = (JSONArray) JSONArray.toJSON(friendRequests);
            String base64 = Base64Encoder.encode(jsonArray.toString());
            response.getWriter().write(base64);
            return;
        }
    }
}
